package com.effectivsocial.rest.controller;
/**
 * 
 */

import java.util.List;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import lombok.extern.slf4j.Slf4j;

import org.apache.cxf.jaxrs.ext.MessageContext;
import org.springframework.social.twitter.api.DirectMessage;

import com.effectivsocial.rest.service.TwitterDirectMessageOperationsService;
import com.effectivsocial.rest.twitter.domain.SimpleDirectMessage;
import com.effectivsocial.rest.util.TwitterFactory;

/**
 * @author Dhrubo
 * 
 */

@Named
@Slf4j
@Path("v1/twitterservice/directmessageoperations")
public class TwitterDirectMessageController {
	
	//OK
	@POST
	@Path("/deletedirectmessage/{messageId}")
	public void deleteDirectMessage(@PathParam("messageId") long messageId, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		service.deleteDirectMessage(messageId);

	}
	
	//OK
	@GET
	@Path("/getdirectmessage/{messageId}")
	public SimpleDirectMessage getDirectMessage(@PathParam("messageId") long messageId, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		
		DirectMessage directMessage = service.getDirectMessage(messageId);
		
		return TwitterFactory.getSimpleDirectMessage(directMessage);
	}
	
	
	//OK
	@GET
	@Path("/getalldirectmessagesreceived")
	public List<SimpleDirectMessage> getDirectMessagesReceived(@Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));	
		List<DirectMessage> directMessages = service.getDirectMessagesReceived();
		
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		
		return TwitterFactory.getSimpleDirectMessages(directMessages);
	}
	
	//OK
	@GET
	@Path("/getdirectmessagesreceivedpagewise/{page}/{pageSize}")
	public List<SimpleDirectMessage> getDirectMessagesReceived(@PathParam("page")int page, @PathParam("pageSize") int pageSize, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		List<DirectMessage> directMessages = service.getDirectMessagesReceived(page, pageSize);
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		
		return TwitterFactory.getSimpleDirectMessages(directMessages); 
	}
	
	
	//NOT OK
	//@GET
	//@Path("/getdirectmessagesreceived/{page}/{pageSize}/{sinceId}/{maxId}")
	public List<SimpleDirectMessage> getDirectMessagesReceived(int page,
			int pageSize, long sinceId, long maxId, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));		
		List<DirectMessage> directMessages = service.getDirectMessagesReceived(page, pageSize,sinceId,maxId);
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		
		return TwitterFactory.getSimpleDirectMessages(directMessages); 
	}
	
	//OK
	@GET
	@Path("/getalldirectmessagessent")
	public List<SimpleDirectMessage> getDirectMessagesSent(@Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		List<DirectMessage> directMessages = service.getDirectMessagesSent();
		
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		return TwitterFactory.getSimpleDirectMessages(directMessages);
	}
	
	//OK
	@GET
	@Path("/getdirectmessagessentpagewise/{page}/{pageSize}")
	public List<SimpleDirectMessage> getPagedDirectMessagesSent(@PathParam("page") int page, @PathParam("pageSize") int pageSize, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		List<DirectMessage> directMessages = service.getDirectMessagesSent(page,pageSize);
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		return TwitterFactory.getSimpleDirectMessages(directMessages);
	}
	
	//NOT OK
	//@GET
	//@Path("/getdirectmessagessent/{page}/{pageSize}/{sinceId}/{maxId}")
	public List<SimpleDirectMessage> getDirectMessagesSent(@PathParam("page") int page, @PathParam("pageSize") int pageSize,
			@PathParam("sinceId") long sinceId, @PathParam("maxId") long maxId, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		List<DirectMessage> directMessages = service.getDirectMessagesSent(page,pageSize,sinceId,maxId);
		if(log.isDebugEnabled()){
			log.debug("Direct Messages = {}", directMessages);
			if(directMessages!=null){
				log.debug("# Direct Messages = {}", directMessages.size());
			}
		}
		
		return TwitterFactory.getSimpleDirectMessages(directMessages);
	}
	
	//OK
	@POST
	@Path("/senddirectmessage/{toScreenName}/{text}")
	public SimpleDirectMessage sendDirectMessage(@PathParam("toScreenName") String toScreenName, @PathParam("text") String text, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));
		DirectMessage directMessage = service.sendDirectMessage(toScreenName,text);
		return TwitterFactory.getSimpleDirectMessage(directMessage);
	}
	
	//OK
	@POST
	@Path("/senddirectmessageToId/{toUserId}/{text}")
	public SimpleDirectMessage sendDirectMessage(@PathParam("toUserId") long toUserId, @PathParam("text") String text, @Context MessageContext context) {
		TwitterDirectMessageOperationsService service = new TwitterDirectMessageOperationsService(TwitterFactory.getTwitter(context));	
		DirectMessage directMessage = service.sendDirectMessage(toUserId,text);
		return TwitterFactory.getSimpleDirectMessage(directMessage);
	}
	
	

}
